package com.example.train_operate.ws.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

/**
 * @author 21609
 * WebRTC 信令消息
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SignalMessage {

    /**
     * 消息类型：join, offer, answer, candidate, leave, error, joined, user_left
     */
    private String type;

    /**
     * 房间id
     */
    private String roomId;

    /**
     * 发送者id
     */
    private String senderId;

    /**
     * 接收者id
     */
    private String targetId;

    /**
     * 消息数据载荷
     */
    private Map<String, Object> data;

    /**
     * 创建错误信息
     */
    public static SignalMessage createError(String code, String message){
        SignalMessage msg = new SignalMessage();
        msg.setType("error");
        msg.setData(Map.of("code", code, "message", message));
        return msg;
    }

    /**
     * 创建加入成功消息
     */
    public static SignalMessage createJoined(String roomId, String senderId,
                                             List<String> participants,
                                             boolean isInitiator) {
        SignalMessage msg = new SignalMessage();
        msg.setType("joined");
        msg.setRoomId(roomId);
        msg.setSenderId(senderId);
        msg.setData(Map.of(
                "participants", participants,
                "isInitiator", isInitiator
        ));
        return msg;
    }

    /**
     * 创建用户离开消息
     */
    public static SignalMessage createUserLeft(String roomId, String userId) {
        SignalMessage msg = new SignalMessage();
        msg.setType("user_left");
        msg.setRoomId(roomId);
        msg.setSenderId(userId);
        msg.setData(Map.of("userId", userId));
        return msg;
    }



}
